package org.fing.edu.uy.esb.adaptative.ws.egov.bps;

import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import javax.xml.ws.Endpoint;
import javax.xml.ws.soap.Addressing;

import org.fing.edu.uy.esb.adapative.ws.testing.PrintName;
import org.fing.edu.uy.esb.adaptative.ws.utils.WsOptionProces;


@Addressing()
@WebService(serviceName="BPSWS")
@SOAPBinding(parameterStyle=ParameterStyle.WRAPPED, style=Style.DOCUMENT, use=Use.LITERAL)
public class BPSWS {

	private Endpoint endpoint;
	private Endpoint endpoint2;
	private final int version = 1;
	
	@WebMethod(exclude=true)
	public void publish(int port , String wsName){
		try{
			InetAddress address = InetAddress.getLocalHost();			
			String url = "http://" + address.getHostName() + ":" + port + "/" + wsName;
			endpoint = Endpoint.publish(url, this);
			System.out.println("Service v"+ version +" published at:" + url);
			url = "http://localhost:" + port + "/" + wsName;
			endpoint2  = Endpoint.publish(url, this);
			System.out.println("Service v"+ version +" published at:" + url);
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Exception in publish " + this.getClass().getSimpleName(), ex);
		}
	}
	
	@WebMethod(exclude=true)
	public void unpublish(){
		try{
			if(endpoint!=null && endpoint.isPublished()){
				endpoint.stop();
				endpoint2.stop();
				System.out.println("Service stopped");
			}
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Exception in unpublish " + this.getClass().getSimpleName(), ex);
		}
	}
	
	@WebMethod(exclude=true)
	public Endpoint getEndpoint() {
		return endpoint;
	}
	
	

	/**
	 * 
	 * Es la misma que la del DGIWS.
	 * 
	 * @param emp
	 * @return RUT de la empresa generada
	 */
	@WebMethod(action="http://dnic.egov.ws.adaptative.esb.uy.edu.fing.org/BPSWS/registroEmpresaNuevaRequest")
	public String registroEmpresaNueva(EmpresaBPS emp){	
		PrintName.printMethodName();
		
		System.out.println("AlDiaBPS = " + emp.isAlDiaBPS());
		System.out.println("AlDiaDGI = " + emp.isAlDiaDGI());
		System.out.println("Ciudad = " + emp.getCiudad());
		System.out.println("Departamento = " + emp.getDepartamento());
		System.out.println("Direccion = " + emp.getDireccion());
		System.out.println("EmpleadosCant = " + emp.getEmpleadosCant());
		System.out.println("NroBPS = " + emp.getNroBPS());
		System.out.println("RUT =" + emp.getRUT());
		
		String result = "empresa registrada (BPS)";
		WsOptionProces.processBPS();
		return result;
	}
    
	
	/**
	 * 
	 * @param RUT identificador de la empresa
	 * @return Objeto que representa la empresa con el rut, null en otro caso
	 */
	@WebMethod(action="http://dnic.egov.ws.adaptative.esb.uy.edu.fing.org/BPSWS/obtenerInfoEmpresaRequest")
	public EmpresaBPS obtenerInfoEmpresa(String RUT,String nuevoParam){
		PrintName.printMethodName();
		
		EmpresaBPS empresa = new EmpresaBPS();
		empresa.setAlDiaBPS(true);
		empresa.setAlDiaDGI(true);
		empresa.setCiudad("ciudad (BPS)");
		empresa.setDepartamento("departamento (BPS)");
		empresa.setDireccion("direccion (BPS)");
		empresa.setEmpleadosCant(1000);
		empresa.setNroBPS("nroBPS (BPS)");
		empresa.setRUT(RUT + "(BPS)");
		
		WsOptionProces.processBPS();
		return empresa;					
	}	
	
	
	
	/**
	 * 
	 * @param CI = [ ].[   ].[   ]-[ ]
	 * @return retorna un objeto que representa a la persona.
	 */
	@WebMethod(action="http://dnic.egov.ws.adaptative.esb.uy.edu.fing.org/BPSWS/obtenerPersonaRequest")
	public PersonaBPS obtenerPersona(String millones,String miles, String cientos ){
		PrintName.printMethodName();

		PersonaBPS persona = null;
		
		Pattern patronMillones = Pattern.compile("^(\\d)");
		Matcher matcherMillones = patronMillones.matcher(millones);	
		Pattern patronMiles= Pattern.compile("^(\\d\\d\\d)");
		Matcher matcherMiles = patronMiles.matcher(miles);
		Pattern patronCientos= Pattern.compile("^(\\d\\d\\d)");
		Matcher matcherCientos = patronCientos.matcher(cientos);
		Pattern patronDigVerif = Pattern.compile("^(\\d)");
		
		if (matcherMillones.find() && matcherMiles.find() && matcherCientos.find() ) {
			millones = matcherMillones.group(1);
			miles = matcherMiles.group(1);
			cientos = matcherCientos.group(1);
			
			persona = new PersonaBPS();
			persona.setApellido1("apellido1 (BPS)");
			persona.setApellido2("apellido2 (BPS)");
			persona.setCI(millones + "." + miles + "." + cientos + "-" );
			persona.setFirmaCedula("");
			persona.setNombre1("nombre1 (BPS)");
			persona.setNombre2("nombre2 (BPS)");
			persona.setSexo("sexo (BPS)");		
		}
		WsOptionProces.processBPS();
		return persona;
	}
	
	
	/**
	 * 
	 * @param Parametro de prueba
	 * @return Objeto que representa la empresa con el rut, null en otro caso
	 */
	@WebMethod(action="http://dnic.egov.ws.adaptative.esb.uy.edu.fing.org/BPSWS/operacionNuevaRequest")
	public String operacionNueva(String nuevoParam){
		PrintName.printMethodName();				
		WsOptionProces.processBPS();
		return "Nueva operacion procesada";					
	}
	
	
}
